package com.fenghongzhang.a1903;

import android.content.Context;
import android.content.SharedPreferences;

public class SpUtils {

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    //防止创建对象
    private SpUtils(){
        sharedPreferences = App.context.getSharedPreferences("user",Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

    }
    private static SpUtils spUtils = null;

    public static SpUtils getInstance(){
        if(spUtils == null){
//            new SpUtils();
            synchronized (SpUtils.class){//不同
                spUtils = new SpUtils();
            }
        }
        return spUtils;
    }


    public void put(String key,Object o){
        if(o instanceof String ){
            editor.putString(key, (String) o);
        }else if(o instanceof Integer){
            editor.putInt(key, (Integer) o); //int integer 区别  基本  对象-> 自动 拆箱 装箱
        }else if(o instanceof Boolean){
            editor.putBoolean(key, (Boolean) o);
        }
        editor.commit();
    }

    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    public SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }
}
